<template>
  <BasicModal @register="register" :title="title" @ok="handleSubmit" width="30%">
    <template #title>
      {{ title }}
      <span v-if="title == '修改状态'" style="font-size: 14px">
        【{{ value.orderPic.cusPicno }}-{{ value.orderPic.picno }}-{{ value.orderPic.picname }}】 +
        【
        <DictLabel :dictType="'m8_task_status'" :dictValue="value.taskStatus" :defaultValue="'0'" />
        】
      </span>
    </template>

    <div v-if="title == '修改状态'" style="color: #1890ff"> 修改后状态： </div>
    <BasicForm @register="registerForm" />
    <p v-if="title == '修改加工时长'" style="color: red; margin-left: 20px">
      提示：请修改加工时长</p
    >
    <p v-if="title == '指定机床'" style="color: red; margin-left: 20px">
      提示：直接填写机床序号即可，如：1！</p
    >
    <p v-if="title == '设定优先级'" style="color: red; margin-left: 20px">
      提示：优先级和加工顺序越小越先进行加工(1-99999)!</p
    >
    <div> </div>
    <div v-if="title == '修改状态'" style="color: red">
      <div>注意事项：</div>
      <div>1、待处理-->暂停/准备就绪/完成</div>
      <div>2、准备就绪-->待处理/暂停/完成</div>
      <div>3、暂停-->待处理/准备就绪/完成</div>
      <div>4、处理中、已完成不能修改状态，如需修改，请到对应机床处理</div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'WorkCalendarModal',
    // components: { ADatePicker: DatePicker },
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  // import { officeTreeData } from '/@/api/sys/office';
  import { updateSjjgTime, setMachine, setFirstLevel, changeStatus } from '/@/api/m8/machine/task';
  import { DictLabel } from '/@/components/Dict';
  import { officeTreeData } from '/@/api/sys/office';

  const emit = defineEmits(['success', 'register']);
  const { showMessage } = useMessage();
  // import { DatePicker } from 'ant-design-vue';
  let title = ref<string>('');
  let value = ref<any>();

  let showOverForm = ref(false);

  const [register, { closeModal, setModalProps }] = useModalInner(async (e) => {
    // , { closeModal, setModalProps }
    value.value = e;
    title.value = e.title;

    await resetFields();

    let inputFormSchemas: FormSchema[] = [];
    if (e.title == '修改状态') {
      inputFormSchemas = [
        {
          label: '',
          field: 'changeStatus',
          component: 'RadioGroup',
          componentProps: {
            dictType: 'm8_task_status',
            onChange: (e: ChangeEvent) => {
              showOverForm.value = e === '3';
              console.log(showOverForm.value);
              updateSchema([
                { field: 'manCodes', ifShow: showOverForm.value },
                { field: 'iqty', ifShow: showOverForm.value },
              ]);
            },
          },
          required: true,
        },
        {
          label: '完工数',
          field: 'iqty',
          component: 'Input',
          required: true,
          ifShow: showOverForm.value,
        },
        {
          label: '负责人',
          field: 'manCodes',
          fieldLabel: 'manNames',
          component: 'TreeSelect',
          componentProps: {
            api: officeTreeData,
            params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
            immediate: true,
            canSelectParent: false,
            maxlength: 64,
          },
          required: true,
          ifShow: showOverForm.value,
        },
      ];
    }
    if (e.title == '修改加工时长') {
      inputFormSchemas = [
        {
          label: '',
          field: 'sjjgtime',
          component: 'Input',
          componentProps: {
            maxlength: 100,
          },
          required: true,
        },
      ];
    }
    if (e.title == '指定机床') {
      inputFormSchemas = [
        {
          label: '',
          field: 'machine',
          component: 'Input',
          componentProps: {
            maxlength: 100,
          },
          required: true,
        },
      ];
    }

    if (e.title == '设定优先级') {
      inputFormSchemas = [
        {
          label: '',
          field: 'firstLevel',
          component: 'Input',
          componentProps: {
            maxlength: 100,
          },
          required: true,
        },
      ];
    }
    setProps({
      labelWidth: 120,
      schemas: inputFormSchemas,
      baseColProps: { lg: 24, md: 24 },
    });
  });

  async function handleSubmit() {
    console.log(value.value);
    try {
      const data = await validate();
      setModalProps({ confirmLoading: true });
      if (title.value == '修改状态') {
        const params: any = {
          selIds: value.value.id,
          ...data,
        };
        const res = await changeStatus(params);
        showMessage(res.message);
        setTimeout(closeModal);
        emit('success', '');
      }
      if (title.value == '修改加工时长') {
        const params: any = {
          selIds: value.value.id,
          ...data,
        };
        const res = await updateSjjgTime(params);
        showMessage(res.message);
        setTimeout(closeModal);
        emit('success', '');
      }
      if (title.value == '指定机床') {
        const params: any = {
          selIds: value.value.id,
          ...data,
        };
        const res = await setMachine(params);
        showMessage(res.message);
        setTimeout(closeModal);
        emit('success', '');
      }
      if (title.value == '设定优先级') {
        const params: any = {
          selIds: value.value.id,
          ...data,
        };
        const res = await setFirstLevel(params);
        showMessage(res.message);
        setTimeout(closeModal);
        emit('success', '');
      }

      // const data = await validate();
      // setModalProps({ confirmLoading: true });
      // const params: any = {
      //   selIds: value.value.id,
      //   ...data
      // };
      // // console.log('submit', params, data, record);
      // const res = await testTreeSave(params, data);
      // showMessage(res.message);
      // setTimeout(closeModal);
      // emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  const [registerForm, { resetFields, validate, setProps, updateSchema }] = useForm();
</script>
<style scoped>
  .ant-picker {
    width: 80%;
  }
</style>
